home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / TinyMeter / Source / TinyMeter_main / idle.c < prev    next >
C/C++ Source or Header  |  1997-02-27  |  5KB  |  216 lines

  1. /*****************************************************************
  2.  
  3.     SystemIdler V1.0 (Idle counter module for TinyMeter 3.6)  by
  4.     Tinic Urou in 1995, FreeWare, orginal code by Thomas Radtke.
  5.     Use this at your own risk. Please leave me a mail if you are
  6.     using this code in your programs:
  7.  
  8.     EMail: tinic@tinic.mayn.sub.de
  9.  
  10.     I modified the code from cpuload2. The routines to count the
  11.     maximum  are  much  more  better I think, since they use the
  12.     exact same  number  of  cycles  as  the  normal  idle  count
  13.     routine.
  14.     On a A4000/030, disabling the startup-sequence, you will now
  15.     get  1%  usage.  With  all my tools I get 9-12% usage. These
  16.     results should be correct.
  17.  
  18.     Bugs: None known.
  19.     ¯¯¯¯
  20.     Invoking:
  21.     ¯¯¯¯¯¯¯¯
  22.     init_idle(); to setup the idle task
  23.     free_idle(); to remove the idle Task
  24.  
  25.     unsigned long idle;      is the actual idlecount
  26.     unsigned long maximum;   is the maximum idlecount
  27.  
  28.     To get f.ex. the percentage of system usage simply use:
  29.  
  30.     __________________________________________________________
  31.  
  32.     extern unsigned long maximum,idle;
  33.  
  34.     showusage()
  35.     {
  36.         int percent,n;
  37.  
  38.         if(init_idle())
  39.         {
  40.         for(n=0;n<25;n++)
  41.         {
  42.             percent=(int)((idle*100)/maximum);
  43.             printf("%d percent free\n",percent);
  44.             Delay(50L);
  45.         }
  46.         free_idle();
  47.         }
  48.     }
  49.  
  50.     ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  51.  
  52.     Used compiler:  gcc  2.7.0  with  libnix  1.0  Look  at  the
  53.     makefile for the used options.
  54.  
  55. *******************************************************************/
  56.  
  57. #include <intuition/IntuitionBase.h>
  58. #include <exec/nodes.h>
  59. #include <exec/tasks.h>
  60. #include <libraries/dos.h>
  61. #include <exec/types.h>
  62. #include <exec/memory.h>
  63. #include <stdlib.h>
  64. #include <signal.h>
  65. #include <exec/libraries.h>
  66. #include <dos/dos.h>
  67. #include <clib/all_protos.h>
  68.  
  69. struct          Task *task2,*met;
  70.  
  71. BOOL            quit_setidle,
  72.         quit_getidle,
  73.         start_count=TRUE;
  74.  
  75. unsigned long   maximum,
  76.         cnt,
  77.         idle;
  78.  
  79. __saveds void getidle(void)
  80. {
  81.     struct          timeval updateval;
  82.     struct MsgPort  *timerport;
  83.     struct          timerequest *tr;
  84.     struct          Task *met2;
  85.  
  86.     /* use "__geta4 getidle()" for DICE or SAS and remove geta4(); */
  87.  
  88.     met2=FindTask(NULL);
  89.  
  90.     if ((timerport=(struct MsgPort *)CreatePort (0,0)))
  91.     {
  92.     if ((tr=(struct timerequest *)CreateExtIO(timerport,sizeof(struct timerequest))))
  93.     {
  94.         if ((OpenDevice (TIMERNAME,UNIT_MICROHZ,(struct IORequest *)tr,0))!=0)
  95.         {
  96.         DeleteExtIO((struct IORequest *)tr);
  97.         DeletePort(timerport);
  98.         goto error;
  99.         }
  100.     }
  101.     else
  102.     {
  103.         DeletePort(timerport);
  104.         goto error;
  105.     }
  106.     }
  107.     else goto error;
  108.  
  109.     updateval.tv_secs =1;
  110.     updateval.tv_micro=0;
  111.  
  112.     while((met2->tc_SigRecvd & SIGBREAKF_CTRL_D)==0)
  113.     {
  114.     /* signal setidle()-task, that we can start counting */
  115.  
  116.     start_count=FALSE;
  117.  
  118.     cnt=0;
  119.     tr->tr_node.io_Command=TR_ADDREQUEST;
  120.     tr->tr_time=updateval;
  121.     DoIO((struct IORequest *)tr);
  122.     idle=cnt;
  123.  
  124.     /* check if we have to setup maximum */
  125.     if(maximum==0)
  126.     {
  127.         maximum=idle;
  128.         Signal(met,SIGBREAKF_CTRL_D);
  129.     }
  130.     }
  131.  
  132.     CloseDevice((struct IORequest *)tr);
  133.     DeleteExtIO((struct IORequest *)tr);
  134.     DeletePort(timerport);
  135.  
  136.     error:
  137.  
  138.     quit_getidle=FALSE;
  139.     start_count =FALSE;
  140.  
  141.     idle   =100000;    /* to avoid divisions by zero from the application */
  142.     maximum=100000;
  143.  
  144.     /* Do nothing and wait for DeleteTask() */
  145.  
  146.     Wait(0L);
  147.  
  148. }
  149.  
  150. __saveds void setidle(void)
  151. {
  152.     char *taskname_1 = "CPU_GET";
  153.     struct Task *task;
  154.  
  155.     /* use "__geta4 setidle()" for DICE or SAS and remove geta4(); */
  156.  
  157.     if(met=FindTask(NULL))
  158.     if(task=(struct Task *)CreateTask(taskname_1,127,getidle,4096))
  159.     {
  160.     quit_getidle=TRUE;
  161.  
  162.     /* Wait for beginning. Allocating a timerequest may take a while */
  163.  
  164.     while (start_count) cnt=0;                                         
  165.  
  166.     /* maximum counter */
  167.     while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  168.     SetSignal(0,SIGBREAKF_CTRL_D);
  169.  
  170.     met->tc_Node.ln_Pri=-127;
  171.  
  172.     /* idle counter */
  173.     while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  174.  
  175.     met->tc_Node.ln_Pri=0;
  176.  
  177.     Signal(task,SIGBREAKF_CTRL_D);
  178.     while(quit_getidle) cnt=0;
  179.  
  180.     /* remove getidle()-task */
  181.  
  182.     Forbid();
  183.     DeleteTask(task);
  184.     Permit();
  185.     }
  186.  
  187.     idle   =100000;    /* to avoid divisions by zero from the       */
  188.     maximum=100000;    /* application, if creation of task failed.  */
  189.  
  190.     quit_setidle=FALSE;
  191.     Wait(0L);
  192. }
  193.  
  194. struct Task *init_idle(void)
  195. {
  196.     char *taskname_2 = "CPU_SET";
  197.     if( task2=(struct Task *)CreateTask(taskname_2,126,setidle,4096))
  198.     {
  199.     Delay(50L); /* To avoid the use of idle and maximum before they're initialized */
  200.     }
  201.     return(task2);
  202. }
  203.  
  204. void free_idle(void)
  205. {
  206.     quit_setidle=TRUE;
  207.     Signal(task2,SIGBREAKF_CTRL_D);
  208.     while(quit_setidle) Delay(10L);
  209.  
  210.     /* remove setidle()-task */
  211.  
  212.     Forbid();
  213.     DeleteTask(task2);
  214.     Permit();
  215. }
  216.